package zhengyanze.com.aipoemmax.guide.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.widget.Toast;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import zhengyanze.com.aipoemmax.R;
import zhengyanze.com.aipoemmax.guide.Constant;
import zhengyanze.com.aipoemmax.guide.adapter.MainAdapter;
import zhengyanze.com.aipoemmax.guide.bean.LeiXingBean;
import zhengyanze.com.aipoemmax.guide.bean.PoemBean;

public class PoemListActivity extends AppCompatActivity {
    public static final String POEM_AUTHOR_KEY = "poem.author.key";
    private static final String TAG = "PoemListActivity";

    ArrayList<PoemBean> mPoemBeans;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    private MainAdapter mainAdapter;
    private String moreLoad;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_poem_list);
        ButterKnife.bind(this);
        LeiXingBean leiBean = getIntent().getParcelableExtra(POEM_AUTHOR_KEY);
        Log.e(TAG, "onCreate: " + leiBean.getLink());
        mPoemBeans = new ArrayList<>();
        initView();
        getUrl(leiBean.getLink());
    }

    public void initView() {
        setSupportActionBar(toolbar);
        setTitle("呵呵");
        rvList.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
        mainAdapter = new MainAdapter(mPoemBeans);
        rvList.setAdapter(mainAdapter);

        //加载更多
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                refreshLayout.finishLoadmore(2000);
                String moreU = Constant.URL.GUSHI_BASE + moreLoad;
                initData(moreU);
            }
        });
    }

    /**
     * 获取网络数据
     */
    public void initData(final String url) {
//        DialogManager.showDialog(this, "加载中...");
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Document document = Jsoup.connect(url).get();
                    Element main3 = document.getElementsByClass("main3").first();
                    Element left = main3.getElementsByClass("left").first();
                    Elements sons = left.getElementsByClass("sons");

                    ArrayList<PoemBean> tempArray = new ArrayList<>();
                    for (Element e : sons) {
                        Element cont = e.getElementsByClass("cont").first();
                        Log.e(TAG, "run: " + e.toString());
                        Element p = cont.getElementsByTag("p").first();
                        Element a = p.getElementsByTag("a").first();
                        String title = a.text();
                        String link = Constant.URL.GUSHI_BASE + a.attr("href").toString();
//                        Log.e(TAG, "标题是: " + title);
//                        Log.e(TAG, "超链接是: " + link);

                        Element source = cont.getElementsByClass("source").first();
//                        Log.e(TAG, "作者是: " + source.text());

                        Element contson = cont.getElementsByClass("contson").first();
                        String content = contson.text();
//                        Log.e(TAG, "内容是: " + content.toString());

                        tempArray.add(new PoemBean(title, source.text(), content, link));
                    }
                    //获取加载更多链接
                    Element pages = left.getElementsByClass("pages").last();
                    Element linkA = pages.getElementsByTag("a").last();
                    if (linkA == null) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(PoemListActivity.this, "没有更多结果了", Toast.LENGTH_SHORT).show();
                            }
                        });
                        moreLoad = null;
                    } else {
                        moreLoad = linkA.attr("href").toString();
                    }

                    mPoemBeans.addAll(tempArray);
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            mainAdapter.notifyDataSetChanged();
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    /**
     * 获取更多诗词URL
     * @param url
     */
    public void getUrl(final String url) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Document document = Jsoup.connect(url).get();
                    Element main3 = document.getElementsByClass("main3").first();
                    Element left = main3.getElementsByClass("left").first();
                    Element last = left.getElementsByClass("title").last();
                    Element a = last.getElementsByTag("a").first();
//                    Log.e(TAG, "run: " + Constant.URL.GUSHI_BASE + a.attr("href").toString());
                    final String url = Constant.URL.GUSHI_BASE + a.attr("href").toString();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            initData(url);
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
